/**
 * Created by walkman on 2017/7/1.
 */

//提交登录
/*$(function () {

 //登录按钮
 $('#btnLogin').bind('click', loginUser);

 function loginUser() {

 // if ($("#password2").val() != "" && $.trim($("#password2").val()).length < 6 || $.trim($("#password2").val()).length > 20) {
 //   // errorTip();
 //   // $("#error_tip").html("密码请输入6-20个字符");
 //   return false;
 // }
 // ;
 // $("#username").val($.trim($("#ajax_phone").val()));
 var data = {
 'userCode': $.trim($("#userCode").val()),
 'password': $.md5($.trim($("#userPwd").val())),
 'verify': $.trim($("#verify").val()),
 'terminalType': 3
 //,
 // 'verifyCode': $('#ajax_phone_code').val(),
 // 'channelCode': 'shake'
 };
 $.ajax({
 async: false,
 type: 'post',
 url: "/user/loginUser",
 dataType: 'json',
 data: data,
 success: function (result) {

 if (result.retCode == '1') {
 // success(data.msg, function () {
 //   location.href = "/";
 // })
 location = "index.html";
 } else {
 // error(data.msg, function () {
 //   obj.bind('click', login)
 // });
 }
 }
 })
 }

 // function login() {
 //   var username = $('#username').val(),
 //     pwd = $('#pwd').val(),
 //     verify = $('#verify').val();
 //   var obj = $('#jax');
 //   obj.unbind('click');
 //   $.post("", {username: username, pwd: pwd, verify: verify}, function (data) {
 //     $('#codeImg').attr('src', "/captcha.html");
 //     if (data.code == 1) {
 //       success(data.msg, function () {
 //         location.href = "/";
 //       })
 //     } else {
 //       error(data.msg, function () {
 //         obj.bind('click', login)
 //       });
 //     }
 //   })
 // }
 });

 $(function () {

 // //图片验证码正确提示元素
 // var $code_state = $("#codeState");
 // $form[0].reset();

 //验证码
 var src = server + "/captcha/gen-vcode?",
 $valid_img = $("#code_img");
 $valid_img.attr("src", src);

 $valid_img.click(function () {
 $valid_img.attr("src", src + Math.random());
 $("#verify").val("");
 });

 //回车转tab(按tabindex顺序)
 $("#loginForm input").on("keydown.enter_convert", function (e) {

 if (e.which == 13) {
 var tabindex = parseInt(this.getAttribute('tabindex')) + 1;
 if (tabindex > 2) {
 return true;
 }
 $("#registerForm input[tabindex='" + tabindex + "']").focus();
 return false;
 }
 });

 $("input[name=vcode]").keyup(function () {
 $.ajax({
 async: false,
 type: 'get',
 url: server + "/captcha/check-vcode?vcode=" + $(this).val(),
 dataType: 'json',
 success: function (data) {
 if (data.result) {
 //$('#sendVeryCode').removeAttr('disabled').removeClass('disable_btn');
 }
 }
 })
 });

 });*/
/**
 * Created by cj on 2017/7/16.
 */
//验证码
var src = "/captcha/gen-vcode?",
  $valid_img = $("#code_img");
$valid_img.attr("src", src);
$valid_img.click(function () {
  $valid_img.attr("src", src + Math.random());
  $("#verify_").val("");
});
//验证码验证
var vCodeCheck = null;
$("#verify_").keyup(function () {
  $.ajax({
    async: false,
    type: 'get',
    url: "/captcha/check-vcode?code=" + $(this).val(),
    dataType: 'json',
    success: function (json) {
      if (json.data.passed) {
        regHide();
      }
      vCodeCheck = json.data.passed;
    }
  })
});
$("#verify_").blur(function () {
  if (vCodeCheck == false) {
    regErr('验证码错误');
    $valid_img.attr("src", src + Math.random());
    //$("#verify_").val("");
  }
});
var reg = {
  userAccount: /^[a-z0-9\u2E80-\u9FFF]{4,16}$/,
  pwd: /^[0-9a-zA-Z]{6,16}$/,
  VerifyCode: /^[0-9a-zA-Z]{4}$/
};
//登录事件
function login() {
  var userName = $.trim($('#userCode').val());
  if (userName == '') {
    regErr('账户不能为空');
    return;
  }
  var len = nameLength(userName);
  if (!userName.match(reg.userAccount) || len < 4 || len > 16) {
    regErr('账户为4-16字节汉字、小写字母或者数字组合');
    return;
  }
  var pwd1 = $.trim($('#userPwd').val());
  if (pwd1 == '') {
    regErr('密码不能为空');
    return;
  } else if (!pwd1.match(reg.pwd)) {
    regErr('密码格式为6-16位数字或者字母组合(大、小写)');
    return;
  }
  var verifyCode = $.trim($('#verify_').val());
  if (verifyCode == '') {
    regErr('验证码不能为空');
    return;
  } else if (!verifyCode.match(reg.VerifyCode)) {
    regErr('验证码格式为4位数字或字母组合，不区分大小写');
    return;
  }
  //console.log(vCodeCheck);
  if (vCodeCheck) {
    regHide();
    //传递注册后自动登陆数据
    var data = {
      'userCode': userName,
      'password': $.md5(pwd1),
      'verify': verifyCode,
      'terminalType': 3
    }
    //请求接口
    $.ajax({
      async: false,
      type: 'post',
      url: "/user/loginUser",
      dataType: 'json',
      data: data,
      success: function (json) {
        //console.log(json);
        if (json.retCode == '1') {
          regHide();
          //console.log('我是跳登录成功后的页面');
          location = "index.html";
        } else {
          regErr(json.retMsg);
        }
      }
    })
  }
}

//字符串
function nameLength(str) {
  if (!str) {
    return false;
  } else {
    var realLength = 0, len = str.length, charCode = -1;
    for (var i = 0; i < len; i++) {
      charCode = str.charCodeAt(i);
      if (charCode >= 0 && charCode <= 128) realLength += 1;
      else realLength += 2;
    }
    return realLength;
  }
}
//reg error
function regErr(text) {
  $('.caution').css('display', 'block').find('span.errorMsg').html(text);
}
function regHide() {
  $('.caution').css('display', 'none');
}
